package com.wcfsy.wechat.wx.um.controller;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.wcfsy.wechat.wx.um.entity.User;
import com.wcfsy.wechat.wx.um.service.IUserConService;
import com.wcfsy.wechat.wx.util.JsonMessage;

@Controller
@RequestMapping("/cms/user")
public class UserController extends AbstractXssDefenderController {
	private static Logger logger = LoggerFactory
			.getLogger(UserController.class);
	@Resource
	private IUserConService userConService;

	@RequestMapping("/view/page")
	public ModelAndView pre_find() {
		return new ModelAndView("operator/manage");
	}

	@RequestMapping({ "/modify_pwd/page" })
	public String preModifyPwd() {
		return "operator/modify_password";
	}

	@RequestMapping({ "/modify_pwd" })
	@ResponseBody
	public JsonMessage modifyPwd(String oldPwd, String newPwd) {
		JsonMessage jsonMessage = new JsonMessage();
		//User _user = (User) SecurityManager.getSessionAttribute("SESSION_USER");
		String userName ="";
		User user = new User(userName, newPwd);
		try {
			this.userConService.changePwd(user, oldPwd);
			jsonMessage.setSuccess("succeed");
			jsonMessage.setMessage("修改成功");
		} catch (Exception e) {
			logger.error("", e);
			jsonMessage.setSuccess("error");
			jsonMessage.setMessage("旧密码错误");
		}
		return jsonMessage;
	}

}
